<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- Copyright © 1988-2023 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Funding Free Software", the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below).  A copy of the license is included in the section entitled
"GNU Free Documentation License".

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU
     software.  Copies published by the Free Software Foundation raise
     funds for GNU development. -->
<title>Type Information (GNU Compiler Collection (GCC) Internals)</title>

<meta name="description" content="Type Information (GNU Compiler Collection (GCC) Internals)">
<meta name="keywords" content="Type Information (GNU Compiler Collection (GCC) Internals)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<link href="index.html" rel="start" title="Top">
<link href="Option-Index.html" rel="index" title="Option Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="index.html" rel="up" title="Top">
<link href="Plugins.html" rel="next" title="Plugins">
<link href="Header-Dirs.html" rel="prev" title="Header Dirs">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span.r {font-family: initial; font-weight: normal; font-style: normal}
span:hover a.copiable-link {visibility: visible}
ul.mark-bullet {list-style-type: disc}
-->
</style>


</head>

<body lang="en">
<div class="chapter-level-extent" id="Type-Information">
<div class="nav-panel">
<p>
Next: <a href="Plugins.html" accesskey="n" rel="next">Plugins</a>, Previous: <a href="Header-Dirs.html" accesskey="p" rel="prev">Standard Header File Directories</a>, Up: <a href="index.html" accesskey="u" rel="up">Introduction</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h2 class="chapter" id="Memory-Management-and-Type-Information"><span>23 Memory Management and Type Information<a class="copiable-link" href="#Memory-Management-and-Type-Information"> &para;</a></span></h2>
<a class="index-entry-id" id="index-GGC"></a>
<a class="index-entry-id" id="index-GTY"></a>

<p>GCC uses some fairly sophisticated memory management techniques, which
involve determining information about GCC&rsquo;s data structures from GCC&rsquo;s
source code and using this information to perform garbage collection and
implement precompiled headers.
</p>
<p>A full C++ parser would be too complicated for this task, so a limited
subset of C++ is interpreted and special markers are used to determine
what parts of the source to look at.  All <code class="code">struct</code>, <code class="code">union</code>
and <code class="code">template</code> structure declarations that define data structures
that are allocated under control of the garbage collector must be
marked.  All global variables that hold pointers to garbage-collected
memory must also be marked.  Finally, all global variables that need
to be saved and restored by a precompiled header must be marked.  (The
precompiled header mechanism can only save static variables if they&rsquo;re
scalar. Complex data structures must be allocated in garbage-collected
memory to be saved in a precompiled header.)
</p>
<p>The full format of a marker is
</p><div class="example smallexample">
<pre class="example-preformatted">GTY (([<var class="var">option</var>] [(<var class="var">param</var>)], [<var class="var">option</var>] [(<var class="var">param</var>)] ...))
</pre></div>
<p>but in most cases no options are needed.  The outer double parentheses
are still necessary, though: <code class="code">GTY(())</code>.  Markers can appear:
</p>
<ul class="itemize mark-bullet">
<li>In a structure definition, before the open brace;
</li><li>In a global variable declaration, after the keyword <code class="code">static</code> or
<code class="code">extern</code>; and
</li><li>In a structure field definition, before the name of the field.
</li></ul>

<p>Here are some examples of marking simple data structures and globals.
</p>
<div class="example smallexample">
<pre class="example-preformatted">struct GTY(()) <var class="var">tag</var>
{
  <var class="var">fields</var>...
};

typedef struct GTY(()) <var class="var">tag</var>
{
  <var class="var">fields</var>...
} *<var class="var">typename</var>;

static GTY(()) struct <var class="var">tag</var> *<var class="var">list</var>;   /* <span class="r">points to GC memory</span> */
static GTY(()) int <var class="var">counter</var>;        /* <span class="r">save counter in a PCH</span> */
</pre></div>

<p>The parser understands simple typedefs such as
<code class="code">typedef struct <var class="var">tag</var> *<var class="var">name</var>;</code> and
<code class="code">typedef int <var class="var">name</var>;</code>.
These don&rsquo;t need to be marked.
</p>
<p>However, in combination with GTY, avoid using typedefs such as
<code class="code">typedef int_hash&lt;&hellip;&gt; <var class="var">name</var>;</code>
for these generate infinite-recursion code.
See <a class="uref" href="https://gcc.gnu.org/PR103157">PR103157</a>.
Instead, you may use
<code class="code">struct <var class="var">name</var> : int_hash&lt;&hellip;&gt; {};</code>,
for example.
</p>
<p>Since <code class="code">gengtype</code>&rsquo;s understanding of C++ is limited, there are
several constructs and declarations that are not supported inside
classes/structures marked for automatic GC code generation.  The
following C++ constructs produce a <code class="code">gengtype</code> error on
structures/classes marked for automatic GC code generation:
</p>
<ul class="itemize mark-bullet">
<li>Type definitions inside classes/structures are not supported.
</li><li>Enumerations inside classes/structures are not supported.
</li></ul>

<p>If you have a class or structure using any of the above constructs,
you need to mark that class as <code class="code">GTY ((user))</code> and provide your
own marking routines (see section <a class="ref" href="User-GC.html">Support for user-provided GC marking routines</a> for details).
</p>
<p>It is always valid to include function definitions inside classes.
Those are always ignored by <code class="code">gengtype</code>, as it only cares about
data members.
</p>

<ul class="mini-toc">
<li><a href="GTY-Options.html" accesskey="1">The Inside of a <code class="code">GTY(())</code></a></li>
<li><a href="Inheritance-and-GTY.html" accesskey="2">Support for inheritance</a></li>
<li><a href="User-GC.html" accesskey="3">Support for user-provided GC marking routines</a></li>
<li><a href="GGC-Roots.html" accesskey="4">Marking Roots for the Garbage Collector</a></li>
<li><a href="Files.html" accesskey="5">Source Files Containing Type Information</a></li>
<li><a href="Invoking-the-garbage-collector.html" accesskey="6">How to invoke the garbage collector</a></li>
<li><a href="Troubleshooting.html" accesskey="7">Troubleshooting the garbage collector</a></li>
</ul>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Plugins.html">Plugins</a>, Previous: <a href="Header-Dirs.html">Standard Header File Directories</a>, Up: <a href="index.html">Introduction</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>
